package service

import (
	"context"
	"diygw-ui-go/internal/v1/dao"
	"diygw-ui-go/internal/v1/model/admin"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/util/gconv"
	"time"
)

type sysPost struct{}

// 对象实例化
func SysPost() *sysPost {
	return &sysPost{}
}

// 获取列表不分页
func (s *sysPost) GetList(ctx context.Context, in admin.SysPostlistInput) (out admin.SysPostlistOutput, err error) {
	m := dao.SysPost.Ctx(ctx).OmitEmpty().Where("delete_time is null")
	if in.Status != "" {
		m = m.Where("status =?", in.Status)
	}
	if err = m.Scan(&out.List); err != nil {
		return
	}
	out.Total, err = m.Count()
	return
}

// 获取列表分页
func (s *sysPost) GetListPage(ctx context.Context, in admin.SysPostlistInput) (out admin.SysPostlistOutput, err error) {
	m := dao.SysPost.Ctx(ctx).OmitEmpty().Where("delete_time is null")
	if in.Status != "" {
		m = m.Where("status =?", in.Status)
	}
	if in.PostId > 0 {
		m = m.Where("post_id= ?", in.PostId)
	}
	if err = m.Page(in.PageNum, in.PageSize).Scan(&out.List); err != nil {
		return
	}
	out.Total, err = m.Count()
	return
}

// 获取详细信息,缓存10小时
func (s *sysPost) GetOne(ctx context.Context, in admin.SysPostOneInput) (out *admin.SysPostOneInput, err error) {
	err = dao.SysPost.Ctx(ctx).Cache(gdb.CacheOption{
		Duration: time.Hour * 10,
		Name:     "postID-" + gconv.String(in.PostId),
		Force:    false,
	}).Where("post_id", in.PostId).Scan(&out)
	return
}
